/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bandwidthcontroller;

import java.text.DecimalFormat;

/**
 *
 * @author flixnet
 */
public class DummyNetCommandGenerator implements CommandGenerator{
    
    private final String SUDO = "sudo ";
    private final String IPFW = "ipfw ";
    private final String ADD_RULE = "add 100 ";
    private final String ADD_PIPE = "pipe 1 ";
    private final String CONFIGBW = "config bw ";
    private final String DELAY = "delay ";
    private final String OUT = "out dst-ip ";
    private final String IN = "in src-ip ";
    private final String LOSSRATIO = "plr ";
    private final String FLUSH = "-f flush";

    @Override
    public String[] getSetAllBandwidthCommand(BandwidthSettings bw) {
        String src = bw.getSrc_ip();
        if(bw.getSrc_ip().matches("0.0.0.0"))
            src = "any ";
        
        String add_pipe = SUDO + IPFW + ADD_RULE + ADD_PIPE + IN + src ;
        String config_pipe = SUDO + IPFW + ADD_PIPE + CONFIGBW + bw.getBandwidth()+"kbit/s " + DELAY + bw.getDelay()+"ms " + LOSSRATIO + roundTwoDecimalPlaces(Double.parseDouble(bw.getPacket_loss())/100);
        System.out.println(add_pipe);
        System.out.println(config_pipe);
        return new String[]{add_pipe,config_pipe};
    }

    @Override
    public String[] getClearAllBandwidthCommand(BandwidthSettings bw) {
        
        String clearPipe = SUDO + IPFW + ADD_PIPE + FLUSH;
        String clearQ = SUDO + IPFW + "-q " + FLUSH;
        String clearIpfw = SUDO + IPFW + FLUSH;
        return new String[]{clearPipe, clearQ, clearIpfw};
    }
    
    private Double roundTwoDecimalPlaces(Double bandwidth){
            
            DecimalFormat twoplaces = new DecimalFormat("#.##");
            return Double.valueOf(twoplaces.format(bandwidth));
            
    }
    
    
    
}
